﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class MapTouchControl : MonoBehaviour {

	// Use this for initialization

    [SerializeField]
    Text info;

    [SerializeField]
    Slider slider;

    int touchStep=0;

   
    public float stepMinDuration;

    float stepCounter;



    public void ChangeStepMinDuration()
    {

       
        stepMinDuration=slider.value;

        Print("slider:"+stepMinDuration);


    }
	void Update () {


       

        if (touchStep==0)
        {

            if (Input.touchCount==0)
            {


                stepCounter=Mathf.Clamp(stepCounter+Time.deltaTime,0f,stepMinDuration+0.01f);


                if (stepCounter>stepMinDuration)
                {

                    stepCounter=0f;

                    touchStep=1;

                }
                    
                    
            }


        }else if (touchStep==1)
        {
            if (Input.touchCount==1 && Input.GetTouch(0).phase==TouchPhase.Ended)
            {
                

                touchStep=2;

            }


        }

        else if (touchStep==2)
        {


            stepCounter=Mathf.Clamp(stepCounter+Time.deltaTime,0f,stepMinDuration+0.1f);


            if (stepCounter>stepMinDuration)
            {

                stepCounter=0f;

                touchStep=0;

                Print("Single Touch");

            }

        }






        if (Input.touchCount==2)
        {

          
            touchStep=0;

            stepCounter=0;


            Touch touchZero=Input.GetTouch(0);

            Touch touchOne=Input.GetTouch(1);


            if (touchZero.phase==TouchPhase.Moved && touchOne.phase==TouchPhase.Moved)
            {

                float distance=Vector2.Distance(touchOne.position-touchOne.deltaPosition,touchZero.position-touchZero.deltaPosition)-Vector2.Distance(touchOne.position,touchZero.position);

                float angle=Vector2.Angle(touchZero.deltaPosition,touchOne.deltaPosition);


                if (Mathf.Abs(angle) < 30.0f)
                {

                    Vector3 detlaPosition = Camera.main.ScreenToWorldPoint(touchZero.position) - Camera.main.ScreenToWorldPoint(touchZero.position-touchZero.deltaPosition);

                    detlaPosition.z = 0;

                    Camera.main.transform.position -= detlaPosition;

                    //Print("Move Camera");


                }
                else
                {

                    Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize + distance/100f, 1, 6);

                    //Print("Zoom Camera");


                }



            }


        }

      


    

		
	}

    void Print(string msg)
    {


        info.text+=msg+"\n";


        if (info.text.Length>100)
        {
          
            info.text="";

        }


    }
}
